home *** CD-ROM | disk | FTP | other *** search
/ CICA 1995 September (Japanese) / CICA Shareware for Windows CD-ROM (Walnut Creek) (September 1995) (Japanese) (Disc 2).iso / disc2 / nt / source.exe / POSIX / FIND / OPTION.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-23  |  5.8 KB  |  205 lines

  1. /*-
  2.  * Copyright (c) 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Cimarron D. Taylor of the University of California, Berkeley.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #ifndef lint
  38. static char sccsid[] = "@(#)option.c    5.8 (Berkeley) 6/4/91";
  39. #endif /* not lint */
  40.  
  41. #ifdef DF_POSIX    /* DF_MSS */
  42. #include <misc.h>
  43. #include <bsdlib.h>
  44. #endif
  45.  
  46. #include <sys/types.h>
  47. #include <sys/stat.h>
  48. #include <fts.h>
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include "find.h"
  53. #ifndef WIN_NT
  54.  
  55. typedef struct _option {
  56.     char *name;        /* option name */
  57.     enum ntype token;    /* token type */
  58.     PLAN *(*create)();    /* create function */
  59. #define    O_NONE        0x01    /* no call required */
  60. #define    O_ZERO        0x02    /* pass: nothing */
  61. #define    O_ARGV        0x04    /* pass: argv, increment argv */
  62. #define    O_ARGVP        0x08    /* pass: *argv, N_OK || N_EXEC */
  63.     int flags;
  64. } OPTION;
  65. #endif
  66.  
  67. OPTION options[] = {
  68.     "!",        N_NOT,        c_not,        O_ZERO,
  69.     "(",        N_OPENPAREN,    c_openparen,    O_ZERO,
  70.     ")",        N_CLOSEPAREN,    c_closeparen,    O_ZERO,
  71.     "-a",        N_AND,        NULL,        O_NONE,
  72.     "-and",        N_AND,        NULL,        O_NONE,
  73.     "-atime",    N_ATIME,    c_atime,    O_ARGV,
  74.     "-ctime",    N_CTIME,    c_ctime,    O_ARGV,
  75.     "-depth",    N_DEPTH,    c_depth,    O_ZERO,
  76.     "-exec",    N_EXEC,        c_exec,        O_ARGVP,
  77.     "-follow",    N_FOLLOW,    c_follow,    O_ZERO,
  78.     "-fstype",    N_FSTYPE,    c_fstype,    O_ARGV,
  79.     "-group",    N_GROUP,    c_group,    O_ARGV,
  80.     "-inum",    N_INUM,        c_inum,        O_ARGV,
  81.     "-links",    N_LINKS,    c_links,    O_ARGV,
  82.     "-ls",        N_LS,        c_ls,        O_ZERO,
  83.     "-mtime",    N_MTIME,    c_mtime,    O_ARGV,
  84.     "-name",    N_NAME,        c_name,        O_ARGV,
  85.     "-newer",    N_NEWER,    c_newer,    O_ARGV,
  86.     "-nogroup",    N_NOGROUP,    c_nogroup,    O_ZERO,
  87.     "-nouser",    N_NOUSER,    c_nouser,    O_ZERO,
  88.     "-o",        N_OR,        c_or,        O_ZERO,
  89.     "-ok",        N_OK,        c_exec,        O_ARGVP,
  90.     "-or",        N_OR,        c_or,        O_ZERO,
  91.     "-perm",    N_PERM,        c_perm,        O_ARGV,
  92.     "-print",    N_PRINT,    c_print,    O_ZERO,
  93.     "-prune",    N_PRUNE,    c_prune,    O_ZERO,
  94.     "-size",    N_SIZE,        c_size,        O_ARGV,
  95.     "-type",    N_TYPE,        c_type,        O_ARGV,
  96.     "-user",    N_USER,        c_user,        O_ARGV,
  97.     "-xdev",    N_XDEV,        c_xdev,        O_ZERO,
  98.     { NULL },
  99. };
  100. #if WIN_NT
  101.  
  102. int typecompare __P((const void *, const void *));
  103. extern void deglobulate __P((void));
  104. extern pid_t ppid;
  105. extern int globulation;
  106. #endif
  107.  
  108. /*
  109.  * find_create --
  110.  *    create a node corresponding to a command line argument.
  111.  *
  112.  * TODO:
  113.  *    add create/process function pointers to node, so we can skip
  114.  *    this switch stuff.
  115.  */
  116. PLAN *
  117. #if __STDC__
  118. find_create (char ***argvp)
  119. #else
  120. find_create(argvp)
  121.     char ***argvp;
  122. #endif
  123. {
  124.     register OPTION *p;
  125.     PLAN *new;
  126.     char **argv;
  127.     OPTION *option __P((char *));
  128.  
  129.     argv = *argvp;
  130.  
  131.     if ((p = option(*argv)) == NULL) {
  132.         (void)fprintf(stderr, "find: unknown option %s.\n", *argv);
  133. #if WIN_NT
  134.         if (ppid == (pid_t) 1 && globulation == 0)
  135.             deglobulate();
  136. #endif
  137.         exit(EXIT_FAILURE);
  138.     }
  139.     ++argv;
  140.     if (p->flags & (O_ARGV|O_ARGVP) && !*argv) {
  141.         (void)fprintf(stderr,
  142.             "find: %s requires additional arguments.\n", *--argv);
  143. #if WIN_NT
  144.         if (ppid == (pid_t) 1 && globulation == 0)
  145.             deglobulate();
  146. #endif
  147.         exit(EXIT_FAILURE);
  148.     }
  149.  
  150.     switch(p->flags) {
  151.     case O_NONE:
  152.         new = NULL;
  153.         break;
  154.     case O_ZERO:
  155.         new = (p->create)();
  156.         break;
  157.     case O_ARGV:
  158.         new = (p->create)(*argv++);
  159.         break;
  160.     case O_ARGVP:
  161.         new = (p->create)(&argv, p->token == N_OK);
  162.         break;
  163.     }
  164.     *argvp = argv;
  165.     return(new);
  166. }
  167.  
  168. OPTION *
  169. #if __STDC__
  170. option (char *name)
  171. #else
  172. option(name)
  173.     char *name;
  174. #endif
  175. {
  176.     OPTION tmp;
  177. #ifndef WIN_NT
  178.     int typecompare __P((const void *, const void *));
  179. #endif
  180.  
  181.     tmp.name = name;
  182.  
  183.     return ((OPTION *) bsearch(&tmp, options,
  184.         sizeof(options)/sizeof(OPTION), sizeof(OPTION), typecompare));
  185. }
  186.     
  187. int
  188. #if __STDC__
  189. typecompare (const void *a, const void *b)
  190. #else
  191. typecompare(a, b)
  192.     const void *a, *b;
  193. #endif
  194. {
  195. #ifndef WIN_NT
  196.         int ret;
  197.  
  198. #endif
  199. #if DF_POSIX
  200.         if (((OPTION *)a)->name == NULL || ((OPTION *)b)->name == NULL) /* DF_DSC: because strcmp hangs if ptr == NULL */
  201.         return 1;
  202. #endif
  203.     return (strcmp(((OPTION *)a)->name, ((OPTION *)b)->name));
  204. }
  205.